﻿namespace Tszy.Unlimited.Base.Infrastructure.Caching;

/// <summary>
/// 缓存管理器。
/// </summary>
public class CacheManager
{
    #region Members

    /// <summary>
    /// 缓存管理器实例。
    /// </summary>
    private static readonly CacheManager _instance = new();

    /// <summary>
    /// 当前缓存管理器。
    /// </summary>
    private ICacheManager _current = new DefaultCacheManager();

    /// <summary>
    /// 锁对象。
    /// </summary>
    private static readonly object _lockOjbect = new();

    #endregion Members

    #region Properties

    /// <summary>
    /// 当前缓存管理器。
    /// </summary>
    public static ICacheManager Current
    {
        get
        {
            lock (_lockOjbect)
            {
                return _instance.InnerCurrent;
            }
        }
    }

    /// <summary>
    /// 当前缓存管理器。
    /// </summary>
    private ICacheManager InnerCurrent => _current;

    #endregion Properties

    #region Public Methods

    /// <summary>
    /// 设置当前使用的缓存方式。
    /// </summary>
    /// <param name="cacheManager">缓存管理器。</param>
    public static void SetCacheManager(ICacheManager cacheManager)
    {
        _instance.InnerSetCacheManager(cacheManager);
    }

    #endregion Public Methods

    #region Pricate Method

    /// <summary>
    /// 设置当前使用的缓存方式。
    /// </summary>
    /// <param name="cacheManager">缓存管理器。</param>
    private void InnerSetCacheManager(ICacheManager cacheManager)
    {
        _current = cacheManager ?? throw new ArgumentNullException(nameof(cacheManager));
    }

    #endregion Pricate Method
}
